package com.yxq.mdt.app.net;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.yxq.mdt.app.BuildConfig;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class FastJsonConverterFactory extends Converter.Factory {

    public static FastJsonConverterFactory create() {
        return new FastJsonConverterFactory();
    }

    /**
     * 需要重写父类中responseBodyConverter，该方法用来转换服务器返回数据
     */
    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new FastJsonResponseBodyConverter<>(type);
    }

    /**
     * 需要重写父类中responseBodyConverter，该方法用来转换发送给服务器的数据
     */
    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new FastJsonRequestBodyConverter<>();
    }

    class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
        private final Type type;

        public FastJsonResponseBodyConverter(Type type) {
            this.type = type;
        }

        /*
         * 转换方法
         */
        @Override
        public T convert(ResponseBody value) {
            BufferedSource bufferedSource = Okio.buffer(value.source());
            String res = null;
            try {
                res = bufferedSource.readUtf8();
            } catch (IOException e) {
                e.printStackTrace();
            }
//            res = "{\"code\":-1,\"msg\":\"登录失败\",\"data\": \"{}\"}";
            Map map = JSON.parseObject(res, Map.class);
            if (BuildConfig.DEBUG) {
                Log.e("返回数据", "Map:" + map.toString());
            }
            //处理Data空串情况
            if (map.containsKey("data")) {
                if (!(map.get("data") instanceof String)) {
                    if (BuildConfig.DEBUG) {
                        Log.e("返回数据", "Data:" + map.toString());
                    }
                    return JSON.parseObject(res, type);
                } else {
                    //data 是空字符串
                    if (BuildConfig.DEBUG) {
                        Log.e("返回数据", "Null Data:" + map.toString());
                    }
                    map.put("data", null);
                    return JSON.parseObject(JSON.toJSONString(map), type);
                }
            } else {
                if (BuildConfig.DEBUG) {
                    Log.e("返回数据", "No Data:" + map.toString());
                }
                return JSON.parseObject(JSON.toJSONString(map), type);
            }
        }
    }

    class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
        private final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");

        @Override
        public RequestBody convert(T value) {
            return RequestBody.create(MEDIA_TYPE, JSON.toJSONBytes(value));
        }
    }

}
